Passed
Pull Request — master (#67)
by Michael
02:01
created

ragraphBeforeButton   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
nop 1
1
import { insertParagraphAtPos } from '../customCommands';
2
3
/**
4
 * @param {function} getView function that returns the current EditorView
5
 * @param {string}   direction should be either 'before' or 'after'
6
 * @return HTMLElement
7
 */
8
function getInsertParagraphButton(getView, direction) {
9
    if (typeof jQuery === 'undefined') {
10
        // early return during js schema testing
11
        return null;
12
    }
13
14
    function dispatchInsertingParagraph(event) {
15
        event.preventDefault();
16
        event.stopPropagation();
17
        const view = getView();
18
        const pos = view.posAtDOM(event.target.parentNode);
19
        const command = insertParagraphAtPos(pos, direction);
20
        command(view.state, view.dispatch);
21
    }
22
23
    const $paragraphButton = jQuery('<button>');
24
    $paragraphButton.css('visibility', 'hidden');
25
    $paragraphButton.on('click', dispatchInsertingParagraph);
26
    $paragraphButton.text('Add paragraph');
27
    const $buttonWrapper = jQuery('<div>').append($paragraphButton);
28
    $buttonWrapper.on('mouseleave', () => {
29
        $paragraphButton.css('visibility', 'hidden');
30
    });
31
    $buttonWrapper.on('mouseenter', (event) => {
32
        const view = getView();
33
        const pos = view.posAtDOM(event.target.parentNode);
34
        const command = insertParagraphAtPos(pos, direction);
35
        if (command(view.state)) {
36
            $paragraphButton.css('visibility', 'visible');
37
        }
38
    });
39
40
    return $buttonWrapper.get(0);
41
}
42
43
/**
44
 * Returns a DOM.Node for a div with a button to insert a new paragraph before the node to which it is attached
45
 *
46
 * This button has functionality to only appear when it is actually available
47
 *
48
 * @param {function} getView function that returns the current EditorView
49
 * @return HTMLElement
50
 */
51
export function getInsertParagraphBeforeButton(getView) {
52
    return getInsertParagraphButton(getView, 'before');
53
}
54
55
/**
56
 * Returns a DOM.Node for a div with a button to insert a new paragraph after the node to which it is attached
57
 *
58
 * This button has functionality to only appear when it is actually available
59
 *
60
 * @param {function} getView function that returns the current EditorView
61
 * @return HTMLElement
62
 */
63
export function getInsertParagraphAfterButton(getView) {
64
    return getInsertParagraphButton(getView, 'after');
65
}
66